import { Tools } from "../../common/Tools";
import { EventManager } from "../../manager/EventManager";
import LvItem from "../../LvItem";
import LvItem_15 from "./LvItem_15";
import { BUNDLE_TYPE_ENUM, EVENT_NAME_ENUM } from "../../base/EnumIndex";
import LevelPre_15 from "./LevelPre_15";
import { UIManager } from "../../manager/UIManager";
import { AudioManager1 } from "../../manager/AudioManager1";


const { ccclass, property } = cc._decorator;

@ccclass
export default class Level_15_176 extends cc.Component {

    level15: LevelPre_15 = null

    start(): void {
        EventManager.ins.on(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finishTween, this)
        this.level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelPre_15);

    }
    setAttachment(node: cc.Node, slotName: string, attachmentName: string) {
        let spine = node.getComponent(sp.Skeleton);
        spine.setAttachment(slotName, attachmentName);
    };

    setBoneActive(node: cc.Node, boneName: string, isActive: boolean) {

        let bone = node.getComponent(sp.Skeleton).findBone(boneName);
        if (!bone) { return; }
        bone.active = isActive;

    };


    async finishTween(finishStr: string) {
        if (typeof finishStr != "string") {
            return;
        }
        if (finishStr.includes("WinOrLoseGame")) {
            this.WinOrLoseGame()
        }
    }

    WinOrLoseGame(){
        if (this.level15.isHaveStates(["1", "2", "3", "4"])) {
            this.level15.runNodeTween(this.node, this.level15.getInterTwForName("winGameAni").interTween);
            return;
        }
        if (this.level15.isHaveNoStates(["4"]) && this.level15.isHaveStates(["1", "2", "3"])) {
            this.gN("nvzhu").getComponent(sp.Skeleton).setAnimation(0, "xuruo", true)
            this.level15.sayNodeQipaoTween({ str: "没有水，活不过三天。", node: "nvSayNode", audio: {resUrl:"没有水，活不过三天。",value:1} })
            if (this.level15.isHaveStates(["10"])) {
                this.scheduleOnce(() => {
                    this.gN("nvzhu").getComponent(sp.Skeleton).setAttachment("待机身体", "铠甲身体");
                    // this.gN("nvzhu").getComponent(sp.Skeleton).findBone("抱腿胳膊后").active = false;
                    // this.gN("nvzhu").getComponent(sp.Skeleton).findBone("抱腿胳膊前").active = false;
                }, 0.001)
            } else {
                this.scheduleOnce(() => {
                    this.gN("nvzhu").getComponent(sp.Skeleton).findBone("顶锅胳膊后").active = false;
                    this.gN("nvzhu").getComponent(sp.Skeleton).findBone("顶锅胳膊前").active = false;
                }, 0.001)
            }
        } else {
            this.gN("qiqiu_1").active = true
            if (this.level15.isHaveNoStates(["1"])) {
                this.gN("大门普通").active = false;
                AudioManager1.playAudio("木门", 1, BUNDLE_TYPE_ENUM.LEVEL);
                this.scheduleOnce(() => {
                    AudioManager1.playAudio("我进来咯？ (女)", 1, BUNDLE_TYPE_ENUM.LEVEL);
                }, 1)
            }
            if (this.level15.isHaveNoStates(["2"])) {
                this.gN("zhuang").getComponent(sp.Skeleton).setAnimation(0, "animation", true);
                this.scheduleOnce(() => {
                    this.gN("zhuang").getComponent(sp.Skeleton).setAnimation(0, "piao", true);
                    this.scheduleOnce(() => {
                        this.gN("zhuang").getComponent(sp.Skeleton).setAttachment("玻璃完整", "玻璃破碎");
                        this.gN("zhuang").getComponent(sp.Skeleton).findBone("加固窗户").active = false;
                    }, 0.001)
                }, 1)
                this.scheduleOnce(() => {
                    AudioManager1.playAudio("玻璃", 1, BUNDLE_TYPE_ENUM.LEVEL);
                }, 0.7)
                this.scheduleOnce(() => {
                    AudioManager1.playAudio("我进来咯？", 1, BUNDLE_TYPE_ENUM.LEVEL);
                }, 1)
            }
            if (this.level15.isHaveNoStates(["3"])) {
                this.gN("墙裂缝破").active = false;
                this.gN("墙底部").active = true;
                AudioManager1.playAudio("墙", 1, BUNDLE_TYPE_ENUM.LEVEL);
                this.scheduleOnce(() => {
                    AudioManager1.playAudio("我进来咯？ (老头)", 1, BUNDLE_TYPE_ENUM.LEVEL);
                }, 1)
            }
            this.gN("nvzhu").getComponent(sp.Skeleton).setAnimation(0, "jingkong", true)
            if (this.level15.isHaveStates(["10"])) {
                this.scheduleOnce(() => {
                    this.gN("nvzhu").getComponent(sp.Skeleton).setAttachment("待机身体", "铠甲身体");
                    this.gN("nvzhu").getComponent(sp.Skeleton).findBone("抱腿胳膊后").active = false;
                    this.gN("nvzhu").getComponent(sp.Skeleton).findBone("抱腿胳膊前").active = false;
                }, 0.001)
            } else {
                this.scheduleOnce(() => {
                    this.gN("nvzhu").getComponent(sp.Skeleton).findBone("顶锅胳膊后").active = false;
                    this.gN("nvzhu").getComponent(sp.Skeleton).findBone("顶锅胳膊前").active = false;
                }, 0.001)
            }
            this.scheduleOnce(() => {
                this.level15.showTipNodeLab("我进来咯？", 2, { str: "我进来咯？" })
            }, 1)
        }
        this.level15.runNodeTween(this.node, this.level15.getInterTwForName("overGameAni").interTween);
    }

    gN(NodeName: string) {
        return this.level15.getNodeForName(NodeName);
    }

    /** 获取世界Scale */
    getWorldScale(tempScale: number = 1, node: cc.Node) {
        if (node.parent) {
            return this.getWorldScale(tempScale / node.parent.scale, node.parent);
        }
        return tempScale;
    };


    onDisable() {
        EventManager.ins.off(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finishTween, this);
    }
}
